package com.tanhua.sso.controller;

import com.tanhua.sso.pojo.User;
import com.tanhua.sso.service.UserService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 此类是用户校验登录的手机号和验证码是否正确,并且提供了校验token的接口,后面会一直被server调用
 * @Author: cloudy
 * @Description:
 * @Date: Created in 9:44 2021/1/2
 * @ModifiedBy:
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param param
     * @return
     */
    @PostMapping("loginVerification")
    public ResponseEntity<Object> loginVerification(@RequestBody Map<String, String> param) {

        try {
            String phone = param.get("phone");
            String verificationCode = param.get("verificationCode");//验证码

            Map login = userService.login(phone, verificationCode);
            if (login != null) {
                return ResponseEntity.ok(login);
            }
        } catch (Exception e) {
            log.error("登录失败", e);
        }

        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("短信验证码发送失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 校验token，根据token查询用户数据
     *
     * @param token
     * @return
     */
    @GetMapping("{token}")
    public User queryUserByToken(@PathVariable("token") String token) {
        return this.userService.queryUserByToken(token);
    }
}
